package com.meilai.project.mapper.business.installOrder;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.meilai.project.entity.business.installOrder.WorkOrderInstallDetail;
import com.meilai.project.vo.app.CompanyOfflineWorkOrderProcessingBuildingDataVO;
import com.meilai.project.vo.app.CompanyOfflineWorkOrderProcessingBuildingRemainDataVO;
import com.meilai.project.vo.business.installOrder.WorkOrderBuidingDetailVO;
import com.meilai.project.vo.business.installOrder.WorkOrderInstallDetailVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Set;

@Mapper
public interface WorkOrderInstallDetailMapper extends BaseMapper<WorkOrderInstallDetail> {

    @Select("<script>" +
            "SELECT  " +
            "b.id AS work_order_id " +
            ",c.id AS contract_id " +
            ",c.daterange_id AS date_range_id " +
            ",a.building_area_id " +
            "FROM work_order_install_detail a " +
            "JOIN work_order b ON a.work_order_id=b.id " +
            "LEFT JOIN bill_sale c ON b.bill_sale_id=c.id " +
            "WHERE b.id IN " +
            "<foreach item=\"item\" index=\"index\" collection=\"workOrderIds\" open=\"(\" separator=\",\" close=\")\"> " +
            "#{item}" +
            "</foreach>" +
            "UNION " +
            "SELECT  " +
            "b.id AS work_order_id " +
            ",bs.id AS contract_id " +
            ",c.daterange_id AS date_range_id " +
            ",a.building_area_id " +
            "FROM work_order_install_detail a " +
            "JOIN work_order b ON a.work_order_id=b.id " +
            "JOIN bill_market_detail c ON b.bill_market_detail_id=c.id " +
            "JOIN bill_sale bs ON c.bill_sale_id = bs.id " +
            "WHERE b.id IN " +
            "<foreach item=\"item\" index=\"index\" collection=\"workOrderIds\" open=\"(\" separator=\",\" close=\")\"> " +
            "#{item}" +
            "</foreach>" +
            "</script>")
    List<CompanyOfflineWorkOrderProcessingBuildingDataVO> queryBuildingsForApp(@Param("workOrderIds") Set<Long> workOrderIds);

    List<CompanyOfflineWorkOrderProcessingBuildingRemainDataVO> queryRemainsForApp(@Param("workOrderIds") Set<Long> workOrderIds);

    List<WorkOrderInstallDetailVO> queryByWorkOrderIdId(@Param("workOrderId") Long workOrderId);

    @Select("select * from work_order_install_detail where work_order_id = #{workOrderId} ")
    List<WorkOrderInstallDetail> listByWorkOrderId(@Param("workOrderId") Long workOrderId);

    @Select("select  a.*,b.`name` as name,c.`name` as media_type_name from `work_order_install_detail` a left join `building_area` b on a.`building_area_id`=b.`id` left join `media_type` c on a.`media_type_id`=c.`id` where ISNULL(a.`deleted_at`) and a.`work_order_id`=#{work_order_id}")
    List<WorkOrderBuidingDetailVO> listWorkOrderBuildingByWorkOrderId(Long work_order_id);

}
